<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageCollectService;
use plugin\ycMassage\app\model\MassageCollectShop;
use plugin\ycMassage\app\model\MassageCollectTechnician;
use plugin\ycMassage\app\model\MassageService;
use plugin\ycMassage\app\model\MassageShop;
use plugin\ycMassage\app\model\MassageUserTechnician;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\State;
use support\Request;
use think\facade\Db;

class CollectController extends BaseController
{
    public function service(Request $request)
    {
        $id = $request->get('id');
        $uid = $request->uid;
        if (!$id) {
            return $this->fail('缺少参数');
        }
        Db::startTrans();
        try {
            $MassageService = MassageService::where(['saas_appid' => $request->saas_appid, 'id' => $id])->find();
            if (!$MassageService) {
                throw new \Exception('服务不存在');
            }
            $MassageCollectService = MassageCollectService::where(['service_id' => $MassageService->id, 'uid' => $uid])->find();
            if ($MassageCollectService) {
                $MassageCollectService->delete();
                $state = 0;
                $MassageService->collect_num = Db::raw('collect_num-1');
            } else {
                $MassageCollectService = new MassageCollectService();
                $MassageCollectService->service_id = $id;
                $MassageCollectService->uid = $uid;
                $MassageCollectService->saas_appid = $request->saas_appid;
                $MassageCollectService->save();
                $state = 1;
                $MassageService->collect_num = Db::raw('collect_num+1');
            }
            $MassageService->save();
            Db::commit();
            return $this->successRes(['state' => $state]);
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
    }
    public function serviceList(Request $request)
    {
        if ($request->get('action') == 'shop') {
            return $this->shopList($request);
        }
        $saas_appid = $request->saas_appid;
        $uid = $request->uid;
        $limit = $request->get('limit', 10);
        $page = $request->get('page', 1);
        $where = [];
        $where[] = ['s.saas_appid', '=', $saas_appid];
        $where[] = ['s.state', '=', State::YES['value']];
        $where[] = ['s.examine', '=', Examine::YES['value']];
        $keywords = $request->get('keywords', '');
        if ($keywords) {
            $where[] = ['s.title', 'like', "%{$keywords}%"];
        }
        $field = [
            's.*'
        ];
        $model = MassageCollectService::alias('c')->where($where);
        $model->join('massage_service s', 's.id = c.service_id');
        $model->field($field);
        $listObj = $model->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
        $list = $listObj->each(function ($item) use ($uid) {
            $item->sales_num = $item->sales_num + $item->virtually_sales_num;
            $item->num = 0;
            if ($item->uts_price > 0) {
                $item->price = $item->uts_price;
            }
            $item->is_collect = 1;
            $item->thumb = UploadService::url($item->thumb);
        });
        return $this->successRes($list);
    }
    public function technician(Request $request)
    {
        $id = $request->get('id');
        $uid = $request->uid;
        if (!$id) {
            return $this->fail('缺少参数');
        }
        Db::startTrans();
        try {
            $MassageUserTechnician = MassageUserTechnician::where(['saas_appid' => $request->saas_appid, 'uid' => $id])->find();
            if (!$MassageUserTechnician) {
                throw new \Exception(MassageReplaceIndustryPersonnelName($request->saas_appid,'技师不存在'));
            }
            $MassageCollectTechnician = MassageCollectTechnician::where(['technician_id' => $MassageUserTechnician->uid, 'uid' => $uid])->find();
            if ($MassageCollectTechnician) {
                $MassageCollectTechnician->delete();
                $state = 0;
                $MassageUserTechnician->collect_num = Db::raw('collect_num-1');
            } else {
                $MassageCollectTechnician = new MassageCollectTechnician();
                $MassageCollectTechnician->technician_id = $id;
                $MassageCollectTechnician->uid = $uid;
                $MassageCollectTechnician->saas_appid = $request->saas_appid;
                $MassageCollectTechnician->save();
                $state = 1;
                $MassageUserTechnician->collect_num = Db::raw('collect_num+1');
            }
            $MassageUserTechnician->save();
            Db::commit();
            return $this->successRes(['state' => $state]);
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
    }
    public function technicianList(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        $keywords = $request->get('keywords');
        $saas_appid = $request->saas_appid;
        $uid = $request->uid;
        $where = [
            'c.saas_appid' => $saas_appid,
            't.examine' => Examine::YES['value'],
            'c.uid' => $uid
        ];
        if ($keywords) {
            $where[] = ['t.name', 'like', "%{$keywords}%"];
        }

        $order = "c.create_time desc";
        $model = MassageCollectTechnician::alias('c')->where($where);
        $model->join('massage_user_technician t', 't.uid = c.technician_id');
        $model->join('massage_user u', 'u.id = t.uid');
        $model->join('massage_shop s', 's.id = t.work_shop_id', 'left');
        $field = [
            't.uid as technician_id,t.work_shop_id,t.saas_appid,t.order_num,t.people_num,t.comment_num,t.collect_num,t.comment_grade',
            't.order_num + t.virtually_order_num as order_num',
            'u.headimg', 'u.nickname',
            's.title as shop_title'
        ];
        $model->field($field);
        $model->order($order);
        $listObj = $model->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
        $shop_title=MassageReplaceIndustryPersonnelName($saas_appid,'个人技师');
        $list = $listObj->each(function ($item)use($shop_title) {
            $item->nickname = base64_decode($item->nickname);
            if (strpos($item->headimg, 'http') !== 0) {
                $item->headimg = UploadService::url($item->headimg);
            }
            if (!$item->shop_title) {
                $item->shop_title = $shop_title;
            }
            # 头像装饰：1，2，3，4
            $item->headimg_decoration = null;
            $item->is_collect = 1;
        });
        return $this->successRes($list);
    }
    public function shop(Request $request)
    {
        $id = $request->get('id');
        $uid = $request->uid;
        if (!$id) {
            return $this->fail('缺少参数');
        }
        Db::startTrans();
        try {
            $MassageShop = MassageShop::where(['saas_appid' => $request->saas_appid, 'id' => $id])->find();
            if (!$MassageShop) {
                throw new \Exception('门店不存在');
            }
            $MassageCollectShop = MassageCollectShop::where(['shop_id' => $MassageShop->id, 'uid' => $uid])->find();
            if ($MassageCollectShop) {
                $MassageCollectShop->delete();
                $state = 0;
                $MassageShop->collect_num = Db::raw('collect_num-1');
            } else {
                $MassageCollectShop = new MassageCollectShop();
                $MassageCollectShop->shop_id = $id;
                $MassageCollectShop->uid = $uid;
                $MassageCollectShop->saas_appid = $request->saas_appid;
                $MassageCollectShop->save();
                $state = 1;
                $MassageShop->collect_num = Db::raw('collect_num+1');
            }
            $MassageShop->save();
            Db::commit();
            return $this->successRes(['state' => $state]);
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
    }
    public function shopList(Request $request)
    {
        $saas_appid = $request->saas_appid;
        $uid = $request->uid;
        $limit = $request->get('limit', 10);
        $page = $request->get('page', 1);
        $where = [];
        $where[] = ['s.saas_appid', '=', $saas_appid];
        $where[] = ['s.state', '=', State::YES['value']];
        $where[] = ['s.examine', '=', Examine::YES['value']];
        $keywords = $request->get('keywords', '');
        if ($keywords) {
            $where[] = ['s.title', 'like', "%{$keywords}%"];
        }
        $field = [
            's.id,s.title, s.logo, s.province, s.city, s.area, s.street, s.latitude, s.longitude, s.collect_num, s.comment_num, s.comment_grade,s.people_num'
        ];
        $model = MassageCollectShop::alias('c')->where($where);
        $model->join('massage_shop s', 's.id = c.shop_id');
        $model->field($field);
        $listObj = $model->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
        $list = $listObj->each(function ($item) use ($uid) {
        });
        return $this->successRes($list);
    }
}
